var courseIds;
$().ready(function () {
    validateRule();
    let id = $("#id").val();
    getMenuTreeData(id);

    $("#requirement").val($("#requirement2").val());
    $("#range").val($("#range2").val());
    $("#time_unit").val($("#time_unit2").val());
    let requirement = $("#requirement").val();

    if(requirement == 1) {
        $(".requirementTime").show();
    }else {
        $(".requirementTime").hide();
    }
    let range = $("#range").val();
    if(range == 1) {
        $("#coursesList").show();
    }else {
        $("#coursesList").hide();
    }
});


$.validator.setDefaults({
    submitHandler: function () {
        getAllSelectNodes();
        update();
    }
});



function update() {
    $('#courseIds').val(courseIds);
    $.ajax({
        cache: true,
        type: "POST",
        url: "/jzweb/coursesClass/savePlan",
        data: $('#signupForm').serialize(),// 你的formid
        async: false,
        error: function (request) {
            parent.layer.alert("Connection error");
        },
        success: function (data) {
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);

            } else {
                parent.layer.alert(data.msg)
            }

        }
    });

}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            startTime: {
                required: true
            },
            endTime: {
                required: true
            },
            requirement: {
                required: true
            },
            range: {
                required: true
            },
        },
        messages: {}
    })
}
layui.use('upload', function () {
    var upload = layui.upload;
    upload.render({
        elem: '#btnUpload',
        url: '/common/sysFile/upload',
        accept: 'images', //只允许上传图片
        acceptMime: 'image/*', //只筛选图片
        size: 1024,
        done: function (r) {
            $("#classPhoto").val(r.data);
            $("#img").attr("src", r.data)
        },
        error: function (r) {
            layer.msg(r.msg);
        }
    });
});

function showTime(){
    let requirement = $("#requirement").val();
    if(requirement == 1){
       $(".requirementTime").show();
    }
    if(requirement == 0){
        $(".requirementTime").hide();
    }
}
function showCourses(){
    let range = $("#range").val();
    if(range == 1){
        $("#coursesList").show();
    }
    if(range == 0){
        $("#coursesList").hide();
    }
}

function getMenuTreeData(id) {
    $.ajax({
        type : "GET",
        url : "/jzweb/coursesClass/tree",
        data: {
            id:id
        },
        success : function(menuTree) {
            // console.log("menuTree",menuTree)
            loadMenuTree(menuTree);
        }
    });
}
function loadMenuTree(menuTree) {
    $('#menuTree').jstree({
        'core' : {
            'data' : menuTree
        },
        "checkbox" : {
            "three_state" : true,
        },
        "plugins" : [ "wholerow", "checkbox" ]
    });
    //$('#menuTree').jstree("open_all");

}
function getAllSelectNodes() {
    var ref = $('#menuTree').jstree(true); // 获得整个树

    courseIds = ref.get_selected(); // 获得所有选中节点的，返回值为数组

    $("#menuTree").find(".jstree-undetermined").each(function(i, element) {
        courseIds.push($(element).closest('.jstree-node').attr("id"));
    });
}

function changValue(){
    let time_requirement = $("#time_requirement").val();
    $("#time_requirement").val(time_requirement.replace(/[^\d]/g,''));
}